﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace PNProject.Core
{
    public class ServerHelper
    {
        public static void DeleteFileOnServer(string fileName)
        {
            try
            {
                File.Delete(HttpContext.Current.Server.MapPath(fileName));
            }
            catch (Exception e)
            {

            }
        }

        public static void SaveFileBaseOnServer(string path, HttpPostedFileBase file, string newName)
        {
            try
            {
                var spath = Path.Combine(HttpContext.Current.Server.MapPath(path), newName);
                file.SaveAs(spath);
            }
            catch (Exception e)
            {

            }
        }

        public static string JoinFileName(List<string> data)
        {
            string result = "";
            foreach (var item in data)
            {
                result = result + item + "*$*";
            }
            return result;
        }

        public static List<string> SplitFileName(string str)
        {
            if (string.IsNullOrEmpty(str))
                return new List<string>();
            var list = str.Split(new string[] { "*$*" }, StringSplitOptions.None).ToList();
            list.RemoveAt(list.Count - 1);
            return list;
        }

        public static List<string> NotExistFileName(List<string> collection, List<string> inputData)
        {
            if (collection == null)
                return new List<string>();
            else
            {
                if (inputData == null)
                    return collection;
                else
                {
                    List<string> result = new List<string>();
                    foreach (var item in collection)
                    {
                        bool flag = true;
                        foreach (var citem in inputData)
                        {
                            if (item == citem)
                            {
                                flag = false;
                                break;
                            }
                        }
                        if (flag)
                            result.Add(item);
                    }
                    return result;
                }
            }
        }
    }
}